<?php

class ErrorController extends Zend_Controller_Action
{

    public function errorAction()
    {
        $front = Zend_Controller_Front::getInstance();
        
        $module = $this->_getParam('module');
        
        $this->view->module = $module;
        
        if($module > '' && $module != 'default'){
            $this->view->url_voltar = $front->getBaseUrl().'/'.$module;
            $this->view->txt_voltar = 'Voltar para o Administrador';
        }
        else{
            $this->view->url_voltar = $front->getBaseUrl().'/';
            $this->view->txt_voltar = 'Voltar para o site';
        }
        
        
        $errors = $this->_getParam('error_handler');
        
        if (!$errors || !$errors instanceof ArrayObject) {
            $this->view->message = 'You have reached the error page';
            return;
        }
        
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $priority = Zend_Log::NOTICE;
                $this->view->message = 'Page not found';
                break;
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $priority = Zend_Log::CRIT;
                $this->view->message = 'Application error';
                break; 
        }
        
        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->log($this->view->message, $priority, $errors->exception);
            $log->log('Request Parameters', $priority, $errors->request->getParams());
        }
        
        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }
        
        $this->view->request   = $errors->request;
        
        
        $session = Zend_Auth::getInstance();
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $post = '';
        if($front->getRequest()->isPost()){
            foreach($_POST as $key => $value) $post .= '&'.$key.'='.$value; 
        }
        
        $data = array(
            'LOG_MSG'   => $errors->exception->getMessage(),
            'LOG_TRCARQ'=> $errors->exception->getTraceAsString(),
            'LOG_MOD'   => $module,
            'LOG_URL'   => $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'],
            'LOG_PST'   => $post,
            'LOG_DAT'   => date('Y-m-d'),
            'LOG_HOR'   => date('H:i:s'),
            'USU_ID'    => isset($session->getStorage()->read()->USU_ID) ? $session->getStorage()->read()->USU_ID:0,
            'LOG_IP'    => $_SERVER["REMOTE_ADDR"], 
        );
        
        $db->insert('LOG_ERROS', $data);
        
        $query = 'SELECT SQL_CACHE * FROM SISTEMA';
        $row = $db->fetchRow($query);
        if($row['SIS_EMLERRSTA'] == 1){
            Zend_Loader::loadClass('envioemail','library');
            $arr['url']         = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
            $arr['ip']         = $_SERVER['REMOTE_ADDR'];
            $arr['mensagem']    = $errors->exception->getMessage();
            $arr['arquivo']     = $errors->exception->getTraceAsString();
            $arr['email']       = $row['SIS_EMLERR'];
            $arr['urlbase']     = $this->_urlbase;
            $ok = envioemail::error($arr); 
        }
        
    }

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }


}

